import 'package:bilibili/basic/http/core/hi_net_adapter.dart';
import 'package:bilibili/basic/http/request/base_request.dart';
import 'package:logger/logger.dart';

import 'dio_adapter.dart';
import 'hi_error.dart';


class HiNet {

  var logger = Logger();

  HiNet._();
  static HiNet? _instance;
  static HiNet getInstance() {
    _instance ??= HiNet._();
    return _instance!;
  }

  Future fire(BaseRequest request) async {

    HiNetResponse? response;
    var error;
    try {
      response = await send(request);
    } on HiNetError catch(e) {
      error = e;
    } catch(e) {
      ///未知异常
      error = e;
    }
    var result = response?.data;
    var code = response?.data['code'];
    if (code != 1) {
      //消息提示
      // HiNetError hiNetError = HiNetError(code!, result.toString(), data: result);
      // throw hiNetError;
    }
    var status = response?.statusCode;
    switch(status) {
      case 200:
        return result;
      case 401:
        throw NeedLogin();
      case 403:
        throw NeedAuth(message: result.toString());
      default:
        throw HiNetError(status ?? -1, result.toString(), data: result);
    }
  }

  Future<dynamic> send<T>(BaseRequest request) async {
    HiNetAdapter adapter = DioAdapter();
    return adapter.send(request);
  }




}